package com.jw.blog.wblog.controller.admin;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jw.blog.annotation.AccessLimit;
import com.jw.blog.wblog.entity.Log;
import com.jw.blog.wblog.entity.custom.ResultBean;
import com.jw.blog.wblog.entity.custom.TimeAndCount;
import com.jw.blog.wblog.service.ILogService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.naming.spi.ResolveResult;
import java.time.ZoneOffset;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author wan Email-wlf18726994755@163.com
 * @version 1.0
 * @className LogControllerAdmin
 * @date 2020/6/21 15:06
 **/
@RestController
public class LogControllerAdmin {
    @Resource
    private ILogService iLogService;



    @GetMapping("/admin/selectUserLogByDate")
    @AccessLimit(seconds=1,maxCount=10)
    public ResultBean<List<TimeAndCount>> selectUserLogByDate(
            Log log,
            @RequestParam(value = "startTime") String startTime,
            @RequestParam(value = "endTime") String endTime
                                                  ){
        Map<String, Object> map = new HashMap<>();
        if (StrUtil.isNotBlank(log.getUserType())) {
            map.put("userType", log.getUserType());
        }
        if (StrUtil.isNotBlank(startTime)) {
            map.put("startTime", startTime);
        }
        if (StrUtil.isNotBlank(endTime)) {
            map.put("endTime", endTime);
        }
        List<TimeAndCount> timeAndCounts = iLogService.selectUserLogByDate(map);
        return new ResultBean<>(timeAndCounts);
    }

    @GetMapping("/admin/selectLogListByDate")
    @AccessLimit(seconds=1,maxCount=10)
    public ResultBean<IPage<Log>> selectLogListByDate(Log log,
                                          @RequestParam(value = "startTime",required = false) String startTime,
                                          @RequestParam(value = "endTime",required = false) String endTime,
                                          @RequestParam(value = "page" ,defaultValue = "1") Integer page,
                                          @RequestParam(value = "pageSize",defaultValue = "9") Integer pageSize){
        QueryWrapper<Log> logQueryWrapper = new QueryWrapper<>();
        if (!StrUtil.hasEmpty(startTime)){
            logQueryWrapper.lambda().ge(Log::getAddTime,startTime);
        }

        if (!StrUtil.hasEmpty(endTime)){
            logQueryWrapper.lambda().le(Log::getAddTime,endTime);
        }
        if (!StrUtil.hasEmpty(log.getIp()) || !StrUtil.hasEmpty(log.getUserType()) || !StrUtil.hasEmpty(log.getDescription())){
            logQueryWrapper.lambda().like(Log::getIp,log.getIp())
                    .or().like(Log::getUserType,log.getUserType())
                    .or().like(Log::getDescription,log.getDescription());
        }
        logQueryWrapper.lambda().orderByDesc(Log::getAddTime);
        //分页显示
        Page<Log> logPage = new Page<>();
        logPage.setCurrent(page);
        logPage.setSize(pageSize);

        return new ResultBean<>(iLogService.page(logPage, logQueryWrapper));
    }
}
